/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage.calibration.visual;

import static org.mopore.jat.gui.SingleRoundCornerRectangleFactory.produceSingleRoundCornerRectangel;

import java.awt.Shape;

import org.mopore.jat.gui.SingleRoundCornerRectangleFactory.CornerType;

public class CalibrationState {

    private static final int WINDOW_HEIGHT = 80;

	private static final int WINDOW_WIDTH = 250;

	private boolean calibtrated;

	private CornerType cornerType;

	private Shape messageWindowShape;
	
	private boolean mouseOver;
	
	private int originX;
	
	private int originY;
	
	private int x;
	
	private int y;
	
	private boolean changed;

	private int screenWidth;

	private int screenHeight;
	

    public CalibrationState( CornerType cornerType, int horizontalOffset, 
    		int verticalOffset, int screenWidth, int screenHeight ) {
    	calibtrated = false;
    	this.cornerType = cornerType;
    	this.screenWidth = screenWidth;
    	this.screenHeight = screenHeight;
    	applyPosition( horizontalOffset, verticalOffset );
    	changed = false;
    	this.messageWindowShape = produceSingleRoundCornerRectangel( 
         		x, y, 
         		WINDOW_WIDTH, WINDOW_HEIGHT, 
         		20, 20, 
         		inverteCornerType( cornerType )
         );
    }


	private void applyPosition(int horizontalOffset, int verticalOffset) {
		switch ( cornerType ) {
			case UPPER_LEFT:{
				originX = 0;
				originY = 0;
				x = originX + horizontalOffset;
				y = originY + verticalOffset;
				break;
			}
			case UPPER_RIGHT:{
				originX = screenWidth;
				originY = 0;
				x = originX - WINDOW_WIDTH - horizontalOffset;
				y = originY + verticalOffset;
				break;
			}
			case LOWER_RIGHT:{
				originX = screenWidth;
				originY = screenHeight;
				x = originX - WINDOW_WIDTH - horizontalOffset;
				y = originY - WINDOW_HEIGHT - verticalOffset;
				break;
			}
			case LOWER_LEFT:{
				originX = 0;
				originY = screenHeight;
				x = originX + horizontalOffset;
				y = originY - WINDOW_HEIGHT - verticalOffset;
				break;
			}			
		}
	}

   
    public void calibtrationFinished() {
    	calibtrated = true;
    }

    public boolean isCalibrated() {
        return calibtrated;
    }


	public CornerType getCornerType() {
		return cornerType;
	}
	
	
	public int getHorizontalOffset() {
		
		int value;
		if ( cornerType == CornerType.UPPER_LEFT  ||
				cornerType == CornerType.LOWER_LEFT ){
			value = x - originX;
		}
		else {
			value = screenWidth - WINDOW_WIDTH - x;
		}
		return value;
	}
	
	
	public Integer getVerticalOffset() {
		
		int value;
		if ( cornerType == CornerType.UPPER_LEFT  ||
				cornerType == CornerType.UPPER_RIGHT ){
			value = y - originY;
		}
		else {
			value = screenHeight - WINDOW_HEIGHT - y;
		}
		return value;
	}


	public boolean isMoveOver() {
		return mouseOver;
	}


	public Shape getMessageWindowShape() {
		
		if ( changed ){
			CornerType rectangleCorner = inverteCornerType( cornerType );
			
			messageWindowShape = produceSingleRoundCornerRectangel( 
	         		x, y, 
	         		WINDOW_WIDTH, WINDOW_HEIGHT, 
	         		20, 20, 
	         		rectangleCorner
	         );
			changed = false;
		}
		
		return messageWindowShape;
	}


	private CornerType inverteCornerType(CornerType cornerType) {
		CornerType invertedCorner;
		switch ( cornerType ) {
			case UPPER_LEFT:{
				invertedCorner = CornerType.LOWER_RIGHT;
				break;
			}
			case UPPER_RIGHT:{
				invertedCorner = CornerType.LOWER_LEFT;
				break;
			}
			case LOWER_RIGHT:{
				invertedCorner = CornerType.UPPER_LEFT;
				break;
			}
			default:{ // LOWER_LEFT
				invertedCorner = CornerType.UPPER_RIGHT;
				break;
			}			
		}
		return invertedCorner;
	}


	public void setMouseOver(boolean mouseOver) {
		this.mouseOver = mouseOver;
	}


	public void applyMove(double newX, double newY) {
		
		if ( newX >= 0 ){
			if ( newX + WINDOW_WIDTH <= screenWidth ){
				x = (int) newX;
			}
		}
		if ( newY >= 0 ){
			if ( newY + WINDOW_HEIGHT <= screenHeight ){
				y = (int) newY;
			}
		}
		changed = true;
	}


}
